package fit.deni.hibernate;

import java.util.Collection;

import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import fit.deni.beans.Klub;
import fit.deni.hibernate.interfaces.KlubService;

public class KlubServiceImpl implements KlubService {

	private Session session;

	public KlubServiceImpl(Session session) {
		this.session = session;
	}

	@CommitAfter
	public void addClub(Klub klub) {
		session.merge(klub);
	}

	public Collection<Klub> getAll() {
		return session.createCriteria(Klub.class).add(Restrictions.eq("deleted", false)).list();
	}

	@CommitAfter
	public void deleteClub(Klub klub) {
		klub.setDeleted(true);
		session.merge(klub);
//		session.delete(klub);
//		session.flush();
	}

	public Klub getClubById(int id) {
		return (Klub) session.createCriteria(Klub.class).add(Restrictions.eq("id", id)).list().get(0);
	}

	public Klub getClubByName(String string) {
		return (Klub) session.createCriteria(Klub.class).add(Restrictions.eq("ime", string)).list().get(0);
	}

}
